package com.student.action;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.student.model.Colleges;
import com.student.service.ProfessionRegisterPaymentService;
import com.student.vo.ProfessionPaymentVo;
import com.student.vo.ProfessionRegisterVo;

/**
 * 关于专业及班级的报道缴费查询的action
 * 
 * @author 林晓滨
 * 
 */
@Component("ProfessionRegisterPaymentAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ProfessionRegisterPaymentAction extends SuperAction {
	private ProfessionRegisterPaymentService professionRegisterPaymentService;

	private Long collegeId; // 学院ID
	private Long professionId; // 专业ID

	private String collegesName_Json; // 所有学院名称
	private String professionRegisterInfo_Json; // 所有专业的报道情况
	private String professionRegisterInfo_Bar; // 所有专业的报道情况柱状图
	private String professionPaymentInfo_Json; // 所有专业的缴费情况
	private String professionPaymentInfo_Bar; // 所有专业的缴费情况柱状图

	private String professionName_Json;
	private String clazzPayment_Json;
	private String clazzRegister_Json;

	private String professionRegister_Pie;
	private String professionPayment_Pie;

	// 初始化学院名称
	public String initCollegeName() throws Exception {
		try {
			Gson gson = new Gson();
			List<Colleges> colleges = professionRegisterPaymentService.getAllCollege();
			Map<String, Long> info = new LinkedMap();
			for (int i = 0; i < colleges.size(); i++) {
				Colleges item = colleges.get(i);
				info.put(item.getCollegesName(), item.getCollegeId());
			}
			collegesName_Json = gson.toJson(info);
			this.logSuccessString("initCollegeName", "初始化学院名称", "初始化成功");
		} catch (Exception e) {
			this.logFailureString("initCollegeName", "初始化学院名称", e);
			e.printStackTrace();
			throw new Exception(e);
		}
		return SUCCESS;
	}

	// 所有专业的报道情况
	public String allProfessionRegisterInfo() throws Exception {
		try {
			if (collegeId == null) {
				Colleges college = (Colleges) ActionContext.getContext().getSession().get("college");
				collegeId = college.getCollegeId();
			}
			initAllProfessionRegisterData(collegeId);
			this.logSuccessString("allProfessionRegisterInfo", "所有专业的报到情况", "获取成功");
		} catch (Exception e) {
			this.logFailureString("allProfessionRegisterInfo", "所有专业的报到情况", e);
			e.printStackTrace();
			throw new Exception(e);
		}
		return SUCCESS;
	}

	// 所有专业的缴费情况
	public String allProfessionPaymentInfo() throws Exception {
		try {
			if (collegeId == null) {
				Colleges college = (Colleges) ActionContext.getContext().getSession().get("college");
				collegeId = college.getCollegeId();
			}
			initAllProfessionPaymentData(collegeId);
			this.logSuccessString("allProfessionPaymentInfo", "所有专业的缴费情况", "获取成功");
		} catch (Exception e) {
			this.logFailureString("allProfessionPaymentInfo","所有专业的缴费情况", e);
			e.printStackTrace();
			throw new Exception(e);
		}
		return SUCCESS;
	}

	// 获取某学院所有专业的报道情况
	private void initAllProfessionRegisterData(Long collegeId) throws Exception {
		try {
			Gson gson = new Gson();
			List<ProfessionRegisterVo> list = professionRegisterPaymentService.getProfessionRegisterVoByCollegeId(collegeId);
			professionRegisterInfo_Json = gson.toJson(list);
			Map<String, Double> map = new LinkedMap();
			for (int i = 0; i < list.size(); i++) {
				ProfessionRegisterVo item = list.get(i);
				map.put(item.getProfessionName(), item.getRegisterRate());
			}
			professionRegisterInfo_Bar = gson.toJson(map);
			this.logSuccessString("initAllProfessionRegisterData", "获取某学院所有专业的报到情况", "获取成功");
		} catch (Exception e) {
			this.logFailureString("initAllProfessionRegisterData", "获取某学院所有专业的报到情况", e);
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	// 获取某学院所有专业的缴费情况
	private void initAllProfessionPaymentData(Long collegeId) throws Exception {
		try {
			Gson gson = new Gson();
			List<ProfessionPaymentVo> list = professionRegisterPaymentService.getProfessionPaymentVoByCollegeId(collegeId);
			professionPaymentInfo_Json = gson.toJson(list);
			Map<String, Double> map = new LinkedMap();
			for (int i = 0; i < list.size(); i++) {
				ProfessionPaymentVo item = list.get(i);
				map.put(item.getProfessionName(), item.getPaymentRate());
			}
			professionPaymentInfo_Bar = gson.toJson(map);
			this.logSuccessString("initAllProfessionPaymentData", "获取某学院所有专业的缴费情况", "获取成功");
		} catch (Exception e) {
			this.logFailureString("initAllProfessionPaymentData", "获取某学院所有专业的缴费情况", e);
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	@Resource
	public void setProfessionRegisterPaymentService(ProfessionRegisterPaymentService professionRegisterPaymentService) {
		this.professionRegisterPaymentService = professionRegisterPaymentService;
	}

	public Long getCollegeId() {
		return collegeId;
	}

	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}

	public Long getProfessionId() {
		return professionId;
	}

	public void setProfessionId(Long professionId) {
		this.professionId = professionId;
	}

	public String getProfessionName_Json() {
		return professionName_Json;
	}

	public void setProfessionName_Json(String professionNameJson) {
		professionName_Json = professionNameJson;
	}

	public String getClazzPayment_Json() {
		return clazzPayment_Json;
	}

	public void setClazzPayment_Json(String clazzPaymentJson) {
		clazzPayment_Json = clazzPaymentJson;
	}

	public String getClazzRegister_Json() {
		return clazzRegister_Json;
	}

	public void setClazzRegister_Json(String clazzRegisterJson) {
		clazzRegister_Json = clazzRegisterJson;
	}

	public String getProfessionRegister_Pie() {
		return professionRegister_Pie;
	}

	public String getProfessionPayment_Pie() {
		return professionPayment_Pie;
	}

	public String getCollegesName_Json() {
		return collegesName_Json;
	}

	public String getProfessionRegisterInfo_Json() {
		return professionRegisterInfo_Json;
	}

	public String getProfessionRegisterInfo_Bar() {
		return professionRegisterInfo_Bar;
	}

	public String getProfessionPaymentInfo_Json() {
		return professionPaymentInfo_Json;
	}

	public String getProfessionPaymentInfo_Bar() {
		return professionPaymentInfo_Bar;
	}

}
